package sample;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: yaung
 * Date: 2016/10/4
 * Time: 21:22
 * To change this template use File | Settings | File Templates.
 */
public class ExcelTest {
    public static void main(String[] args) throws IOException {
        String cellStr = null;
        File excelFile = new File("E:\\zone\\migrate\\excel\\案卷目录（进馆档案）.xls");
        FileInputStream is = new FileInputStream(excelFile);// 获取文件输入流  
        HSSFWorkbook workbook2003 = new HSSFWorkbook(is);// 创建Excel2003文件对象
        HSSFSheet sheet = workbook2003.getSheetAt(0);// 取出第一个工作表，索引是0
        // 开始循环遍历行，表头不处理，从1开始
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            HSSFRow row = sheet.getRow(i);// 获取行对象
            if (row == null) {// 如果为空，不处理
                continue;
            }
// 循环遍历单元格
            for (int j = 0; j < row.getLastCellNum(); j++) {
                HSSFCell cell = row.getCell(j);// 获取单元格对象
                if (cell == null) {// 单元格为空设置cellStr为空串
                    cellStr = "";
                } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理
                    cellStr = String.valueOf(cell.getBooleanCellValue());
                } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理
                    cellStr = cell.getNumericCellValue() + "";
                } else {// 其余按照字符串处理
                    cellStr = cell.getStringCellValue();
                }
                System.out.print(cellStr + " - ");
//// 下面按照数据出现位置封装到bean中
//                if (j == 0) {
//                    student.setName(cellStr);
//                } else if (j == 1) {
//                    student.setGender(cellStr);
//                } else if (j == 2) {
//                    student.setAge(new Double(cellStr).intValue());
//                } else if (j == 3) {
//                    student.setSclass(cellStr);
//                } else {
//                    student.setScore(new Double(cellStr).intValue());
//                }
            }
            System.out.println();
        }
    }
}
